
//here we define the cost for unit and the consumption of oni milks
var cost_oni = 3.5;
var consumo_oni = 8;

$(document).ready(function(){


  $('form.jqtransform').jqTransform({
    imgPath:'jqtransformplugin/img/'
  });  
  var scrollDone = false;
  
  
  $("#friends_list #friends_panel").mCustomScrollbar({
    mouseWheel:true,
    scrollButtons:{
      enable:false
    }
  });


  $('#add_btn_friend').click(function(){    
    $.fancybox({
      'href':'#lightbox_wrap_add_friends',
      'showCloseButton':'false'
    });
  });
  
  $("#send_inv #goal_wrap .jqTransformSelectWrapper ul li a").click(function(e){
    var money_amount = 0.0;
    if($(this).attr("index")==0){
      money_amount = 1500;
    }
    if($(this).attr("index")==1){
      money_amount = 1000;
    }
    if($(this).attr("index")==2){
      money_amount = 500;
    }
    if($(this).attr("index")==3){
      money_amount = 100;
    }
    $("#money").text("$"+money_amount);
    $("#quantity").text(""+Math.floor(money_amount/cost_oni));
    $("#kids").text(""+Math.floor(money_amount/cost_oni/consumo_oni));
    
  });


  $('#lightbox_wrap_add_friends #btn_close_lightbox').click(function(){
    $.fancybox.close();
  });
  $(".checkbox_friend").click(function(e){
    $attr = $("#"+e.target.id).attr("added");
    if($attr){
      $("#"+e.target.id).attr("added", "false");
    }
    else{
      $("#"+e.target.id).attr("added", "true");
    }
  });


  $('#lightbox_wrap_add_friends #btn_add_lightbox').click(function(){
    var add_ids = {};
    var add_names = {};
    var iter = 0;
    var my_id = $("#panel_add .fb_friend_display:first-child").attr("data-uid");
    var my_name = $("#panel_add .fb_friend_display:first-child").attr("data-uname");
    var cad = '<div class="fb_friend_display" data-uid="' + my_id  +
    '" data-uname="'+ my_name  +'" ><div class="over_frame"></div><a><img width="41px" height="41px" alt=""  \n\
src="https://graph.facebook.com/'+ my_id   + '/picture"/></a></div>';
    var friend_ids = $(".checkbox_friend[added='true']");
    $.fancybox.close();
    friend_ids.each(function(){
      add_ids[iter] = $(this).val();
      add_names[iter] = $(this).attr("data-uname");
      cad += '<div class="fb_friend_display" data-uid="' + add_ids[iter] + 
      '"  data-uname="'+ add_names[iter]  +'"  ><div class="over_frame"></div><a><img width="41px" height="41px" alt=""  \n\
src="https://graph.facebook.com/'+ add_ids[iter]+ '/picture"/></a><a class="delete_fb_friend"></a></div>';
      iter++;
    });
    $("#panel_add .fb_friend_display").parent("div").html(cad);
    if(!scrollDone){
      $("#friends_panel").mCustomScrollbar({
        mouseWheel:true,
        scrollButtons:{
          enable:false,
          scrollType:"continuous",
          scrollSpeed:20,
          scrollAmount:40
        },
        advanced:{
          updateOnContentResize: true
        }
      });
      scrollDone = true;
    }
  });

  $(".delete_fb_friend").live("click",function(e){
    
    $(this).parent(".fb_friend_display").remove();
  });


  $('#send_invite_btn').click(function(){

    //var menuId = $("ul.nav").first().attr("id");
    var title = $("input#title_input_form").val();
    var invitation_message  = $("textarea#msj_input").val();
    var goal_date_day = $("select#date_day").val();
    var goal_date_month = $("select#date_month").val();
    var goal_date_year = $("select#date_year").val();
    var goal_quantity = $("select#goal_drop").val();
    var goal_date = goal_date_year + "-" + goal_date_month + "-" + goal_date_day;

    var fb_friend = $("#main_content .fb_friend_display").length;
    var fb_friend_ids = '';

    for (x = 1;x<=fb_friend;x++){
      
      fb_friend_ids += $("#main_content .fb_friend_display:nth-child("+ x +")").attr("data-uid") +
      '---'+$(".fb_friend_display:nth-child("+ x +")").attr("data-uname") + ',';
    }
    

    $.ajax({
      url: "save_user_data.php",
      type: "POST",
      data: {
        'title' : title,
        'invitation_message' : invitation_message,
        'goal_date' : goal_date,
        'goal_quantity' : goal_quantity,
        'guests_ids':fb_friend_ids
      },
      dataType: "JSON",
      success:function(response){
        $("#lightbox_wrap").html(response);
        $.fancybox({
          'href':'#lightbox_wrap',
          'showCloseButton':'false'
        });
      },
      error:function (xhr, ajaxOptions, thrownError){
        $.fancybox({
          'href':'#lightbox_wrap_fail',
          'showCloseButton':'false'
        });
      }
    });

  //    var ajax = new Ajax();
  //    ajax.data = {
  //            'title' : title,
  //            'invitation_message' : invitation_message,
  //            'goal_date_day' : invitation_message,
  //            'goal_date' : goal_date,
  //            'goal_quantity' : goal_quantity
  //          }
  //    ajax.ondone = function(data) {
  //      $.fancybox({
  //        'href':'#lightbox_wrap',
  //        'showCloseButton':'false'
  //      });
  //    }
  //    ajax.onerror = function(data) {
  //      $.fancybox({
  //        'href':'#lightbox_wrap_fail',
  //        'showCloseButton':'false'
  //      });
  //    }
  //
  //    var url='save_user_data.php';
  //    ajax.post(url);

  


   
  });

  $('.checkbox_friend').click(function(e){
     
    })



  $("#btn_close_lightbox").click(function(){
    $.fancybox.close();
  });




});